package com.vincent.testall.util;

import java.io.Serializable;

/**
 * @author wlsonp
 * @param <T>
 */
public class ResponseResult<T> implements Serializable{
	
	private static final String SUCCESS_CODE = "0000000";
	
	private String errorCode;
	private String errorMessage;
	private T data;
	
	public ResponseResult() {
	}
	
	public ResponseResult(ResponseResult<T> old) {
		if (null != old.getErrorCode() ) {
			this.errorCode = new String(old.getErrorCode());
		}
		if (null != old.getErrorMessage() && !"".equals(old.getErrorMessage())) {
			this.errorMessage = new String(old.getErrorMessage());
		}
		if (null != old.getData()) {
			this.data = old.getData();
		}
	}

	
	public String getErrorCode() {
		return errorCode;
	}
	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}
	public String getErrorMessage() {
		return errorMessage;
	}
	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}
	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}

	public static <T> ResponseResult<T> error(String errorCode, Object errorMessage){
		ResponseResult<T> result =	new ResponseResult<>();
		result.setErrorCode(errorCode);
		result.setErrorMessage(errorMessage.toString());
		return result;
	}
	
	public static <T> ResponseResult<T> success(String message, T data){
		ResponseResult<T> result =	new ResponseResult<>();
		result.setErrorCode(SUCCESS_CODE);
		result.setErrorMessage(message);
		result.setData(data);
		return result;
	}
	
	public static <T> ResponseResult<T> empty(){
		ResponseResult<T> result =	new ResponseResult<>();
		result.setErrorCode(SUCCESS_CODE);
		result.setErrorMessage("empty");
		result.setData(null);
		return result;
	}

	
	public boolean isSucceed(){
		return SUCCESS_CODE.equals(errorCode);
	}

	@Override
	public String toString() {
		if (data != null) {
			return "ResponseResult [errorCode=" + errorCode + ", errorMessage=" + errorMessage + ", data=" + data.toString() + "]";
		}
		return "ResponseResult [errorCode=" + errorCode + ", errorMessage=" + errorMessage + "]";
		
	}
	
	/**
	 * 用于处理当返回数据T为null的情况下，明确告诉客户端，此API返回数据为空（void）
	 * @author wilsonp
	 */
	public static class Void{
		private Void(){}
		public static final Void V = null;
	}
}
